title: "Gender diversity analysis" author: A. McSween date: '2022-09-14'


In [ ]:
# Packages
import numpy as np
import pandas as pd
import plotly.express as px # for graphs
In [ ]:
# Import .csv files for plotting

df = pd.read_csv(r"./age_groups.csv")

Plotting results of census data on gender diversity¶

Basic bar graph¶

In [ ]:
# make bar graph
fig = px.bar(
        df,                                         # the data to use
        x = "Age group",                            # xaxis value
        y = "Percentage",                           # yaxis value
        color = "Gender"                            # which values to plot
)
fig.show()

Unstack the bars¶

In [ ]:
fig = px.bar(
        df,                                         # the data to use
        x = "Age group",                            # xaxis value
        y = "Percentage",                           # yaxis value
        color = "Gender",                           # which values to plot
        barmode = "group"                           # group instead of stack values
)
fig.show()

Reformat to make it look nicer¶

I want to remove the legend title. Here the axis font size and angle already looks good! One point for Python (or more honestly, for Plotly).

In [ ]:
fig = px.bar(
        df,                                         # the data to use
        x = "Age group",                            # xaxis value
        y = "Percentage",                           # yaxis value
        color = "Gender",                           # which values to plot
        barmode = "group"                           # group instead of stack values
)
fig.update_layout(legend_title_text="", xaxis_title="")
fig.show()

Add a title and a caption¶

Finally I want to add a title and a caption that notes the source of the data.

In [ ]:
fig = px.bar(
        df,                                         # the data to use
        x = "Age group",                            # xaxis value
        y = "Percentage",                           # yaxis value
        color = "Gender",                           # which values to plot
        barmode = "group",                          # group instead of stack values
        title = "Gender diversity by age group"     # figure title
        )

fig.update_layout(legend_title_text="", xaxis_title="")

fig.add_annotation(text="Source: www150.statcan.gc.ca/n1/daily-quotidien/220427/dq220427b-eng.htm",
                  xref="paper", yref="paper",
                  x=.5, y=-.3, showarrow=False)
fig.show()

Flip and inspect¶

Let's just flip it and inspect to see what it looks like.

In [ ]:
fig = px.bar(
        df,                                         # the data to use
        y = "Age group",                            # xaxis value
        x = "Percentage",                           # yaxis value
        color = "Gender",                           # which values to plot
        barmode = "group",                          # group instead of stack values
        title = "Gender diversity by age group",
        orientation="h"
        )
fig.update_layout(legend_title_text="", xaxis_title="")
fig.add_annotation(text="Source: www150.statcan.gc.ca/n1/daily-quotidien/220427/dq220427b-eng.htm",
                  xref="paper", yref="paper",
                  x=.5, y=-.3, showarrow=False)
fig.update_yaxes(autorange="reversed")
fig.show()